/*==================================================
Project:       Targeting Social Programs
Authors:       Diether W. Beuermann
               Bridget Hoffmann        
               Marco Stampini 
               David L. Vargas
               Diego Vera-Cossio
----------------------------------------------------
Creation Date:    
Modification Date:   
Do-file version:    01
References:          
Output:             
==================================================*/

/*==================================================
        0: Program set up
==================================================*/
drop _all
set varabbrev off	// no variable abbreviations allowed (personal preference)

** source dir
cd "${dir4r}" // tables dir

*----------  1.1. Estimate PMT coefficients for population data
* For convenience all pmt relevant coefficients are availible in an aux dta *
use "${dir3r}/01_survey/survey_targeting_r1_long.dta", clear

*---------- 1.2: Relevant macros
glo losses job_loss ilness death divorce bankrupt theft conflict cut_remittance fire nat_disaster

label var job_loss "Involuntary job loss - Main earner"
label var ilness "Accident or illness"
label var death "Death of a household member"
label var bankrupt "Bankruptcy or business closure"

/*==================================================
			2: Table of stats shocks
==================================================*/

// create any shock variable 
egen any_shock = rowmax($losses)
lab var any_shock "Any shock"
glo losses ${losses} any_shock

// estimate by year
loc k = 0
forv year = 2019/2021{
	loc ++k
	loc j = 0
	loc rowname ""				// An empty local to keep track of var/names
	foreach v of global losses { 	
	loc ++j
	
	// stats
	sum `v' [aw=pondera] if year == `year', detail
	scalar v_mean = r(mean) * 100
	
	// store data in matrix
	matrix output = J(1,1,.)
	matrix output[1,1] = v_mean
	
	if (`j' == 1) 		mat stat = output
	else 				mat stat = stat\output
	
	loc rowname "`rowname' `v'"
	}

if (`k' == 1)       mat tb = stat
else                mat tb = tb , stat  
	
}

matrix rownames tb = `rowname'
matrix list tb 

** Export 
frmttable using T1_shocks_stats, statmat(tb) ///
ctitles( "\% Of sample experiencing:", "2019", "2020", "2021" ) ///
sdec(1) varlabels ///
coljust(l c) ///
spaceht(1) replace tex frag
